package cn.xiaou.draw;

import java.awt.Color;
import java.awt.Component;

import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class MyDefaultTableModel extends DefaultTableModel {
	private static final long serialVersionUID = 1L;
	public boolean isCellEditable(int row, int column) {
		  return false;
	 }
	public void removeAll() {
		while(this.getRowCount()>0){
			this.removeRow(this.getRowCount()-1);
		}
	}
	 public static void setOneRowBackgroundColor(JTable table) {  
	        try {  
	            DefaultTableCellRenderer tcr = new DefaultTableCellRenderer() {  
					private static final long serialVersionUID = 1L;

					public Component getTableCellRendererComponent(JTable table,  
	                        Object value, boolean isSelected, boolean hasFocus,  
	                        int row, int column) {  
						
						Double state = (Double) table.getValueAt(row,6);
						if(state > 0) {
							setBackground(Color.RED);
							setForeground(Color.WHITE);  
						}else if(state < 0) {
							setBackground(Color.GREEN);
							setForeground(Color.BLACK);  
						}else {
							setBackground(Color.BLACK);  
	                        setForeground(Color.WHITE);
						}
	                    return super.getTableCellRendererComponent(table, value,  
	                            isSelected, hasFocus, row, column);  
	                }  
	            };  
	            int columnCount = table.getColumnCount();  
	            for (int i = 0; i < columnCount; i++) {  
	                table.getColumn(table.getColumnName(i)).setCellRenderer(tcr);  
	            }  
	        } catch (Exception ex) {  
	            ex.printStackTrace();  
	        }  
	    }  

}
